import router from '@/router';
import axios from 'axios'
import { ElMessage } from 'element-plus';
let server  = axios.create({
  baseURL: "/api",
  timeout: 5000
})
//axios提供的请求和响应的拦截
server.interceptors.request.use(config => {
  //在发送请求之前做些什么
  config.headers.token = sessionStorage.getItem('token');
  return config;
}, error => {
  //对请求错误做些什么
  return Promise.reject(error);
});
server.interceptors.response.use(response => {
  if(response.data.code === 401){
    ElMessage.error(response.data.msg)
    router.push('/login')
    return;
  }
  if(response.data.code === 403){
    ElMessage.error(response.data.msg)
    return;
  }
  //对响应数据做点什么
  return response.data;
});
export default server